package com.weizai.carshop.controller;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.weizai.carshop.common.enumeration.AppHttpCodeEnum;
import com.weizai.carshop.common.result.PageResult;
import com.weizai.carshop.common.result.ResponseResult;
import com.weizai.carshop.model.dto.QueryShopCateDto;
import com.weizai.carshop.model.dto.QueryShopInfoDto;
import com.weizai.carshop.model.dto.ShopCateInfoDto;
import com.weizai.carshop.model.entiity.GoodsInfoEntity;
import com.weizai.carshop.model.entiity.ShopCateInfoEntity;
import com.weizai.carshop.service.ShopCateInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/admin/shopInfo")
@RequiredArgsConstructor
public class ShopInfoController {

    private final ShopCateInfoService shopCateInfoService;


    /*
     * 查询分类详情
     * */
    @GetMapping
    public ResponseResult queryShopInfo(QueryShopInfoDto dto) {

        PageResult result = shopCateInfoService.queryList(dto);
        return ResponseResult.okResult(result);
    }

    /*
     * 新增或编辑分类详情
     * */
    @PostMapping
    public ResponseResult insert(@RequestBody ShopCateInfoDto dto) {
        shopCateInfoService.insert(dto);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /*
     * 删除接口
     * */
    @DeleteMapping("/{id}")
    public ResponseResult DelShopInfo(@PathVariable Long id) {
        // 判断当前删除是否有关联
        List<GoodsInfoEntity> list = Db.lambdaQuery(GoodsInfoEntity.class).eq(GoodsInfoEntity::getCateId, id).list();

        if(CollUtil.isNotEmpty(list)) {
            return ResponseResult.errorResult(500, "当前分类下有商品，无法删除");
        }
        shopCateInfoService.removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
